home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2000 January / Macworld (2000-01).dmg / Shareware World / Info / For Developers / OSA Script.sea / OSA Script / ExecuteScript.p < prev    next >
Text File  |  1999-10-21  |  4KB  |  97 lines

  1. Program ExecuteOSAScriptWithParams;
  2. {©23/7/99 — Jan Skarbek. http://www.ozemail.com.au/~pbird. pbird@ozemail.com.au}
  3. {You are welcome to distribute this AppleEvent handling sample freely, but must keep the package intact.}
  4.  
  5. {Comments, questions, and suggestions invited.}
  6.  
  7. {An example of how to call a user-defined handler within a script object ('scpt').}
  8.  
  9. {The script, in this case, was copied out of the accompanying compiled script sample, with ResEdit,}
  10. {and placed in the Execute Script.rsrc file.}
  11.  
  12. {This sample only shows how to call a handler with a simple direct parameter: a list. It is possible to call a handler}
  13. {defined by name, rather than by event codes, but that's not covered in this example.}
  14.  
  15. {Note that you should set your 'SIZE' resource to indicate that you accept high-level events, tho it's not}
  16. {strictly necessary if the script exists only as a part of your application and doesn't call any outside processes.}
  17. {Failure to do so will prevent the AEM from replying to you. You should also allocate at least 200KB to the app.}
  18.  
  19. {  Added WRITELN output to display actions and results of program, and added exit when error detected. }
  20. {  Bill Catambay, bill.m.catambay@lmco.com, 10/21/99 }
  21.  
  22. Uses
  23.     Processes, AppleEvents, OSA, ExecuteEventInScript;
  24. Const
  25.     scriptResID = 128;            {the ID of the 'scpt' resource, as copied from the script sample}
  26. Var
  27.     err, ie: OSErr;
  28.     a, b, c, x, y, z: longint;
  29.     desc1, desc2: AEDescList;
  30.     theAEKey: AEKeyWord;
  31.     actualSize: Size;
  32.     typeCode: DescType;
  33.  
  34. Procedure ErrorExit(err: OSErr);
  35.  
  36.     begin
  37.     writeln('ERROR EXCEPTION: Error #',err:1);
  38.     ExitToShell;
  39.     end; { of ErrorExit }
  40.     
  41. Begin
  42. a := 1;
  43. b := 2;
  44. c := 3;
  45. x := 0;
  46. y := 0;
  47. z := 0;
  48. writeln('ExecuteScript Sample Pascal Program');
  49. writeln('ACTION: Pass in parameters a, b and c to target script "The Script to Execute"');
  50. writeln('EXPECTED RESULTS: Script increments values and stores results in x, y and z');
  51. writeln;
  52. err := noErr;
  53. desc1.dataHandle := nil;        {Always null your AEDescs!}
  54. desc2.dataHandle := nil;
  55.  
  56. {create an empty parameter list for the script handler}
  57. writeln('Creating descriptor...');
  58. err := AECreateList(nil, 0, false, desc1);
  59. if err <> noErr then
  60.     ErrorExit(err);
  61.     
  62. {add the parameters to the list}
  63. writeln('Adding arguments to descriptor (a=',a:1,', b=',b:1,', c=',c:1,')...');
  64. err := AEPutPtr(desc1, 0, typeLongInteger, @a, SizeOf(a));
  65. if err <> noErr then
  66.     ErrorExit(err);
  67. err := AEPutPtr(desc1, 0, typeLongInteger, @b, SizeOf(b));
  68. if err <> noErr then
  69.     ErrorExit(err);
  70. err := AEPutPtr(desc1, 0, typeLongInteger, @c, SizeOf(c));
  71. if err <> noErr then
  72.     ErrorExit(err);
  73.  
  74. {execute the handler within the script, whose Class is 'abcd' and whose ID is '1234'}
  75. writeln('Executing script...');
  76. err := ExecuteEventInCompiledScriptResource(scriptResID, desc1, desc2, 'abcd', '1234');
  77. if err <> noErr then
  78.     ErrorExit(err);
  79.  
  80. {extract the result from the executed script. desc2 holds the returned values}
  81. writeln('Extracting results and storing in x, y and z...');
  82. err := AEGetNthPtr(desc2, 1, typeLongInteger, theAEKey, typeCode, @x, SizeOf(x), actualSize);
  83. if err <> noErr then
  84.     ErrorExit(err);
  85. err := AEGetNthPtr(desc2, 2, typeLongInteger, theAEKey, typeCode, @y, SizeOf(x), actualSize);
  86. if err <> noErr then
  87.     ErrorExit(err);
  88. err := AEGetNthPtr(desc2, 3, typeLongInteger, theAEKey, typeCode, @z, SizeOf(x), actualSize);
  89. if err <> noErr then
  90.     ErrorExit(err);
  91.  
  92. {clean up}
  93. ie := AEDisposeDesc(desc1);
  94. ie := AEDisposeDesc(desc2);
  95. writeln;
  96. writeln('RESULTS: x=',x:1,', y=',y:1,', z=',z:1);
  97. end.